package bdd.sql.server.bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.UniqueConstraint;

import enumTypes.Civility;
import enumTypes.Rank;
 
/**
 * Bean User.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
@Entity
@Table(name = "kraken_user", uniqueConstraints = {@UniqueConstraint(columnNames={"mail"})})
public class User {
 
	@Id
	@GeneratedValue
	@Column(name = "id")
	private int user_id;
	
	@Column(name = "last_name", nullable = false)
	private String user_lastname;
	
	@Column(name = "name", nullable = false)
	private String user_name;
	
	@Column(name = "mail", nullable = false, unique = true)
	private String user_mail;
	
	@Column(name = "seed_pwd", nullable = false, unique = true, updatable = false)
	private String user_seed_pwd;
 
	@Column(name = "pwd", columnDefinition = "varchar(255) default 'kraken_pwd'", nullable = false, insertable = true)
	private byte[] user_pwd = USER_PWD_DEFAULT.getBytes();
	
	@Enumerated(EnumType.STRING)
	@Column(name = "rank", nullable = false)
	private Rank user_rank;
	
	@Enumerated(EnumType.STRING)
	@Column(name = "civil", nullable = false)
	private Civility user_civil;
	
	@Column(name = "firstcon", columnDefinition = "int(4) default '1'" ,nullable = false, insertable = true)
	private int user_firstcon = 1;
	
	// implémentation différentes
	@ManyToMany
    @JoinTable(name="user_camera",
                joinColumns={@JoinColumn(name="user_id")},
                inverseJoinColumns={@JoinColumn(name="cam_id")})
    private Set<Camera> cameras = new HashSet<Camera>();
	
	@OneToMany(mappedBy="user")
    private Set<User_Log> logs;
	
	private static final String USER_PWD_DEFAULT = "kraken_pwd";
	
	/**
	 * Constructeur vide.
	 */
	public User() {}
 
	/**
	 * Créer un nouveau User.
	 * @param lastname nom de l'utilisateur
	 * @param name prénom de l'utilisateur
	 * @param mail email de l'utilisateur
	 * @param rank rang de l'utilisateur
	 */
	public User(String lastname, String name, String mail, Rank rank, Civility civil, String seed) {
		this.user_lastname = lastname;
		this.user_name = name;
		this.user_mail = mail;
		this.user_rank = rank;
		this.user_civil = civil;
		this.user_seed_pwd = seed;
	}
	
	public int getUser_id() { return user_id; }
	public void setUser_id(int user_id) { this.user_id = user_id; }

	public String getUser_name() { return user_name; }
	public void setUser_name(String user_name) { this.user_name = user_name; }

	public String getUser_lastname() { return user_lastname; }
	public void setUser_lastname(String user_lastname) { this.user_lastname = user_lastname; }

	public String getUser_mail() { return user_mail; }
	public void setUser_mail(String user_mail) { this.user_mail = user_mail; }

	public byte[] getUser_pwd() { return user_pwd; }
	public void setUser_pwd(byte[] user_pwd) { this.user_pwd = user_pwd; }

	public Rank getUser_rank() { return user_rank; }
	public void setUser_rank(Rank user_rank) { this.user_rank = user_rank; }
	
	public Civility getUser_civil() { return user_civil;	}
	public void setUser_civil(Civility user_civil) { this.user_civil = user_civil;	}

	public int getUser_firstcon() { return user_firstcon; }
	public void setUser_firstcon(int user_firstcon) { this.user_firstcon = user_firstcon; }

	public Set<Camera> getCameras() { return cameras; }
	public void setCameras(Set<Camera> cameras) { this.cameras = cameras; }
	
	public Set<User_Log> getUser_logs() { return logs; }
	public void setUser_logs(Set<User_Log> user_logs) { this.logs = user_logs; }
	
	public String getUser_seedPwd() { return user_seed_pwd; }
	public void setUser_seedPwd(String user_seedPwd) { this.user_seed_pwd = user_seedPwd; }

	/**
	 * Ajout une Camera à l'utilisateur.
	 * @param cam
	 */
	public void addCamera(Camera cam) {
		getCameras().add(cam);
		cam.getUsers().add(this);
	}

	public String getUser_pwd_default() { return USER_PWD_DEFAULT; }
	
	@Override
	public String toString() { return this.user_mail; }
	
}
